//
//  WTBarrageOperation.m
//  WTLiveSDKDemo
//
//  Created by ben on 2016/11/25.
//  Copyright © 2016年 ben. All rights reserved.
//

#import "WTBarrageOperation.h"
#import "WTBarrageService.h"
#import "WTLiveSDKHeaders.h"

@interface WTBarrageOperation ()
@property (nonatomic, getter = isFinished)  BOOL finished;
@property (nonatomic, getter = isExecuting) BOOL executing;
@property (nonatomic,strong) NSArray<WTBarrageSendModel*>* barrages;
@property (nonatomic,copy) NSString*    anchor_id;
@property (nonatomic,copy) WTBarrageCompletionBlock finishedBlock;
@property (nonatomic,strong) WTBarrageService* barrageService;
@end

@implementation WTBarrageOperation
@synthesize finished = _finished;
@synthesize executing = _executing;

+ (instancetype)sendBarrageOperation:(NSArray<WTBarrageSendModel *> *)barrages anchor_id:(NSString *)anchor_id finishedBlock:(WTBarrageCompletionBlock)finishedBlock{
    WTBarrageOperation* op = [[WTBarrageOperation alloc] init];
    op.barrages = barrages;
    op.anchor_id = anchor_id;
    op.finishedBlock = finishedBlock;
    return op;

}


- (instancetype)init{
    
    self = [super init];
    if (self) {
        _executing = NO;
        _finished  = NO;
    }
    return self;
}

- (void)start{
    
    if ([self isCancelled]) {
        self.finished = YES;
        return;
    }
    self.executing = YES;
    
    NSInteger total_num = self.barrages.count;
    
    NSLog(@"弹幕数量:%zd",total_num);
    
    __weak __typeof(self)weakSelf = self;
    [self.barrageService start:@{@"session_token":WTConfig.session_token,@"anchor_id":@([self.anchor_id longLongValue]),@"num":@(total_num)} completionHandler:^(id responseObject, NSError *error) {
        
        if(!error){
            
            NSDictionary* hashValue = responseObject[@"hash"];
            if(hashValue){
                NSNumber* statusCode = hashValue[@"StatusCode"];
                NSNumber* coins = @([hashValue[@"Msg"] longLongValue]);
                if([statusCode integerValue] == 6){
                    if(weakSelf.finishedBlock){
                        weakSelf.finishedBlock(YES,coins);
                    }
                }else {
                    if(weakSelf.finishedBlock){
                        weakSelf.finishedBlock(NO,nil);
                    }
                }
            }
        }else {
            if(weakSelf.finishedBlock){
                weakSelf.finishedBlock(NO,nil);
            }
        }
        weakSelf.finished = YES;
    }];
    
    while (!self.finished) {
        CFRunLoopRun();
    }
}
#pragma mark -  手动触发 KVO
- (void)setExecuting:(BOOL)executing
{
    [self willChangeValueForKey:@"isExecuting"];
    _executing = executing;
    [self didChangeValueForKey:@"isExecuting"];
}

- (void)setFinished:(BOOL)finished
{
    [self willChangeValueForKey:@"isFinished"];
    _finished = finished;
    [self didChangeValueForKey:@"isFinished"];
}


#pragma mark getter
- (WTBarrageService *)barrageService{
    if(!_barrageService){
        _barrageService = [[WTBarrageService alloc] init];
    }
    return _barrageService;
}
@end
